import React, { Component } from 'react';
import { NavBar, Toast } from 'antd-mobile'
import { Button } from 'antd-mobile'
import '../styles/Login.scss'
import service from '../api';
class Login extends Component {
    constructor(props) {
        super(props)
        this.state = {
            loginname: '',
            password: '',

        }
    }
    back() {
        this.props.history.go(-1)
    }
    onChange(e) {
        this.setState({
            [e.target.name]: e.target.type == 'checkbox' ? e.target.checked : e.target.value
        })
    }
    toregister() {
        this.props.history.push('/register')
    }
    async toindex() {
        var res = await service.user.user_login({ loginname: this.state.loginname, password: this.state.password })
        if (res.data.code == 10010) {
            Toast.show(res.data.message)
        } else if (res.data.code == 10011) {
            Toast.show(res.data.message)
        } else if (res.data.code == 200) {
            Toast.show(res.data.message)
            this.props.history.push('/index/home')
            localStorage.setItem('token', res.data.data.token)
            localStorage.setItem('userid', res.data.data.userid)
        }
    }
    render() {
        return (
            <div className='login'>
                <div className="fan">
                    <NavBar onBack={() => { this.back() }}>登录</NavBar>
                </div>
                <div className='deng'>
                    <input type="text" placeholder='手机号/账户名/邮箱' name='loginname' value={this.state.loginname} onChange={(e) => { this.onChange(e) }} />
                </div>
                <div className="deng">
                    <input type="text" placeholder='请输入密码' name='password' value={this.state.password} onChange={(e) => { this.onChange(e) }} />
                </div>
                <div className="btn" onClick={() => { this.toindex() }} >
                    <Button block size='large'onClick={() => { this.toindex() }} >
                        登录
                    </Button>
                </div>
                <div className="reg" onClick={() => { this.toregister() }}>
                    手机快速注册
                </div>
            </div>
        );
    }
}

export default Login;